package com.example.apple.hxbtc.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.apple.hxbtc.R;
import com.example.apple.hxbtc.activity.KlineActivity;
import com.example.apple.hxbtc.bean.DataOne;
import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.apple.hxbtc.R.id.text_one;


/**
 * Created by hpz on 17/2/21.
 * <p>
 * url地址
 * OkCoin: https://data.btcchina.com/data/ticker?market=btccny
 * HuoBi: http://api.huobi.com/staticmarket/detail_btc_json.js
 * BTCC: https://data.btcchina.com/data/ticker?market=btccny
 * CoinBase: https://api.coinbase.com/v2/prices/spot?currency=USD
 * Kraken: https://api.kraken.com/0/public/Spread?pair=XBTUSD
 * ANXPRO: https://anxpro.com/api/2/BTCUSD/money/ticker
 * Korbit: https://api.korbit.co.kr/v1/ticker/detailed?currency_pair=btc_krw
 * BitHumb: https://api.bithumb.com/public/ticker
 */


public class PriceFragment extends Fragment implements View.OnClickListener {


    @BindView(text_one)
    TextView textOne;
    @BindView(R.id.text_buy_one)
    TextView textBuyOne;
    @BindView(R.id.text_sell_one)
    TextView textSellOne;
    @BindView(R.id.text_two)
    TextView textTwo;
    @BindView(R.id.text_buy_two)
    TextView textBuyTwo;
    @BindView(R.id.text_sell_two)
    TextView textSellTwo;
    @BindView(R.id.text_three)
    TextView textThree;
    @BindView(R.id.text_buy_three)
    TextView textBuyThree;
    @BindView(R.id.text_sell_three)
    TextView textSellThree;
    @BindView(R.id.text_four)
    TextView textFour;
    @BindView(R.id.text_buy_four)
    TextView textBuyFour;
    @BindView(R.id.text_sell_four)
    TextView textSellFour;
    @BindView(R.id.text_five)
    TextView textFive;
    @BindView(R.id.text_buy_five)
    TextView textBuyFive;
    @BindView(R.id.text_sell_five)
    TextView textSellFive;
    @BindView(R.id.text_six)
    TextView textSix;
    @BindView(R.id.text_buy_six)
    TextView textBuySix;
    @BindView(R.id.text_sell_six)
    TextView textSellSix;
    @BindView(R.id.text_seven)
    TextView textSeven;
    @BindView(R.id.text_buy_seven)
    TextView textBuySeven;
    @BindView(R.id.text_sell_seven)
    TextView textSellSeven;
    @BindView(R.id.text_eight)
    TextView textEight;
    @BindView(R.id.text_buy_eight)
    TextView textBuyEight;
    @BindView(R.id.text_sell_eight)
    TextView textSellEight;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.price_trade)
    TextView priceTrade;
    private TextView kline;

    private Timer timer;
    static FragmentActivity mActivity;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    String str1 = (String) msg.obj;
                    textOne.setText(str1);
                    break;
                case 1:
                    String str2 = (String) msg.obj;
                    textTwo.setText(str2);
                    break;
                case 2:
                    String str3 = (String) msg.obj;
                    textThree.setText(str3);
                    break;
                case 3:
                    String str4 = (String) msg.obj;
                    textFour.setText(str4);
                    break;
                case 4:
                    String str5 = (String) msg.obj;
                    textFive.setText(str5);
                    break;
                case 5:
                    String str6 = (String) msg.obj;
                    textSix.setText(str6);
                    break;
                case 6:
                    String str7 = (String) msg.obj;
                    textSeven.setText(str7);
                    break;
                case 7:
                    String str8 = (String) msg.obj;
                    textEight.setText(str8);
                    break;
                case 8:
                    String str9 = (String) msg.obj;
                    textBuyOne.setText(str9);
                    break;
                case 9:
                    String str10 = (String) msg.obj;
                    textBuyTwo.setText(str10);
                    break;
                case 10:
                    String str11 = (String) msg.obj;
                    textBuyThree.setText(str11);
                    break;
                case 11:
                    String str12 = (String) msg.obj;
                    textBuyFour.setText(str12);
                    break;
                case 12:
                    String str13 = (String) msg.obj;
                    textBuyFive.setText(str13);
                    break;
                case 13:
                    String str14 = (String) msg.obj;
                    textBuySix.setText(str14);
                    break;
                case 14:
                    String str15 = (String) msg.obj;
                    textBuySeven.setText(str15);
                    break;
                case 15:
                    String str16 = (String) msg.obj;
                    textBuyEight.setText(str16);
                    break;
                case 16:
                    String str17 = (String) msg.obj;
                    textSellOne.setText(str17);
                    break;
                case 17:
                    String str18 = (String) msg.obj;
                    textSellTwo.setText(str18);
                    break;
                case 18:
                    String str19 = (String) msg.obj;
                    textSellThree.setText(str19);
                    break;
                case 19:
                    String str20 = (String) msg.obj;
                    textSellFour.setText(str20);
                    break;
                case 20:
                    String str21 = (String) msg.obj;
                    textSellFive.setText(str21);
                    break;
                case 21:
                    String str22 = (String) msg.obj;
                    textSellSix.setText(str22);
                    break;
                case 22:
                    String str23 = (String) msg.obj;
                    textSellSeven.setText(str23);
                    break;
                case 23:
                    String str24 = (String) msg.obj;
                    textSellEight.setText(str24);
                    break;
                case 24:
                    String str25 = (String) msg.obj;
                    priceTrade.setText(" ¥ "+ str25);
                    break;

            }
            super.handleMessage(msg);
        }

    };


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (FragmentActivity) activity;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initView();

    }

    private void initView() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                sendRequestWithOkHttpOne();

            }
        }, 2000, 10 * 1000);//2000表示开始时2秒后发送一次，10*10000表示每10秒后发送一次
    }

    private void sendRequestWithOkHttpOne() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("https://data.btcchina.com/data/ticker?market=btccny")
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseJSONWithGSONOne(responseData);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void parseJSONWithGSONOne(String jsonData) {
        Gson gson = new Gson();
        DataOne dataOne = gson.fromJson(jsonData, DataOne.class);
        Message message1 = new Message();
        message1.what = 0;
        message1.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message1);

        Message message2 = new Message();
        message2.what = 1;
        message2.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message2);

        Message message3 = new Message();
        message3.what = 2;
        message3.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message3);

        Message message4 = new Message();
        message4.what = 3;
        message4.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message4);

        Message message5 = new Message();
        message5.what = 4;
        message5.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message5);

        Message message6 = new Message();
        message6.what = 5;
        message6.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message6);

        Message message7 = new Message();
        message7.what = 6;
        message7.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message7);

        Message message8 = new Message();
        message8.what = 7;
        message8.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message8);

        Message message9 = new Message();
        message9.what = 8;
        message9.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message9);

        Message message10 = new Message();
        message10.what = 9;
        message10.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message10);

        Message message11 = new Message();
        message11.what = 10;
        message11.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message11);

        Message message12 = new Message();
        message12.what = 11;
        message12.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message12);

        Message message13 = new Message();
        message13.what = 12;
        message13.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message13);

        Message message14 = new Message();
        message14.what = 13;
        message14.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message14);

        Message message15 = new Message();
        message15.what = 14;
        message15.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message15);

        Message message16 = new Message();
        message16.what = 15;
        message16.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message16);

        Message message17 = new Message();
        message17.what = 16;
        message17.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message17);

        Message message18 = new Message();
        message18.what = 17;
        message18.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message18);

        Message message19 = new Message();
        message19.what = 18;
        message19.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message19);

        Message message20 = new Message();
        message20.what = 19;
        message20.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message20);

        Message message21 = new Message();
        message21.what = 20;
        message21.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message21);

        Message message22 = new Message();
        message22.what = 21;
        message22.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message22);

        Message message23 = new Message();
        message23.what = 22;
        message23.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message23);

        Message message24 = new Message();
        message24.what = 23;
        message24.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message24);

        Message message25 = new Message();
        message25.what = 24;
        message25.obj = dataOne.getTicker().getLast();
        mHandler.sendMessage(message25);

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        kline = (TextView) view.findViewById(R.id.kline_button);
        kline.setOnClickListener(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_price, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.button:
                textOne.setText("");
                break;
            case R.id.kline_button:
                Intent intent = new Intent();
                intent.setClass(getActivity(), KlineActivity.class);
                startActivity(intent);
                break;
        }
    }
}
